/*
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.googlecode.guicejunit4.springtx.inner;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.transaction.support.DefaultTransactionStatus;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.googlecode.guicejunit4.core.InjectorProvider;

/**
 * 
 * @author java2enterprise@gmail.com (James Wang)
 * @since Nov 20, 2009
 * 
 */
public class InjectorProviderWithTxManager implements InjectorProvider {
	
	@Override
	public Injector get() {
		return Guice.createInjector(new Module() {
			@Override
			public void configure(Binder binder) {
				binder.bind(PlatformTransactionManager.class).toInstance(txManager());
			}
		});
	}

	private PlatformTransactionManager txManager() {
		return new AbstractPlatformTransactionManager() {
			private static final long serialVersionUID = 7125401213043399214L;

			@Override
			protected void doRollback(DefaultTransactionStatus status) 
				throws TransactionException {
				
				logger.warn("Rollback tx with status : " + status);
			}
			
			@Override
			protected Object doGetTransaction() throws TransactionException {
				return new Object();
			}
			
			@Override
			protected void doCommit(DefaultTransactionStatus status)
				throws TransactionException {

				logger.info("Committing tx with status : " + status);
			}
			
			@Override
			protected void doBegin(Object transaction, TransactionDefinition definition)
				throws TransactionException {
				
				logger.info("Begining tx on target : " + transaction + ", defination : " + definition);
			}
		};
	}

	
}
